#include "interrupt.h"
#include "hw_types.h"
#include "osram96x16x1.h"
#include "menu.h"


//
//	Main menu
//
unsigned char mainMenu[8][32]={

	255, 0, 0, 254, 4, 8, 4, 254, 0, 254, 146, 146, 0, 254, 16, 16, 254,0,  
	254, 16, 124, 130, 130, 124, 0, 0, 4, 254, 0, 0, 0, 255,   					//menu1
	
	255, 0, 0, 254, 4, 8, 4, 254, 0, 254, 146, 146, 0, 254, 16, 16, 254,0,  
	254, 16, 124, 130, 130, 124, 0, 0, 196, 162, 146, 140, 0, 255, 				//menu2
	
	255, 0, 0, 254, 4, 8, 4, 254, 0, 254, 146, 146, 0, 254, 16, 16, 254,0,  
	254, 16, 124, 130, 130, 124, 0, 0, 68, 130, 146, 108, 0, 255, 				//menu3
		
	255, 0, 0, 254, 4, 8, 4, 254, 0, 254, 146, 146, 0, 254, 16, 16, 254,0,  
	254, 16, 124, 130, 130, 124, 0, 0, 32, 40, 254, 32, 0, 255, 				//menu4 
	
	
	0,255, 255, 1, 251, 247, 251, 1, 255, 1, 109, 109, 255, 1, 239, 239, 1,255,  
	1, 239, 131, 125, 125, 131, 255, 255, 251, 1, 255, 255, 255, 0, 			//menu1_inv
	
	0,255, 255, 1, 251, 247, 251, 1, 255, 1, 109, 109, 255, 1, 239, 239, 1,255,  
	1, 239, 131, 125, 125, 131, 255, 255, 59, 93, 109, 115, 255, 0,			//menu2_inv
	
	0,255, 255, 1, 251, 247, 251, 1, 255, 1, 109, 109, 255, 1, 239, 239, 1,255,  
	1, 239, 131, 125, 125, 131, 255, 255, 187, 125, 109, 147, 255, 0,			//menu3_inv
	
	0,255, 255, 1, 251, 247, 251, 1, 255, 1, 109, 109, 255, 1, 239, 239, 1,255,  
	1, 239, 131, 125, 125, 131, 255, 255, 223, 215, 1, 223, 255, 0, 			//menu4_inv
	
	}; 
	
const short int mainMenuL[2]={4,32};
unsigned char *pMainMenu[8];

//
// Sub menu 1
// 
unsigned char subMenu1[6][45] = {
	255,0,24,36,254,36,24,0,78,144,144,126,0,254,16,16,254,0,254,24,20,226,0,
	126,64,126,192,0,254,96,24,254,0,140,82,50,254,0,0,4,254,0,0,0,255, 				//funkciq 1

	255,0,24,36,254,36,24,0,78,144,144,126,0,254,16,16,254,0,254,24,20,226,0,
	126,64,126,192,0,254,96,24,254,0,140,82,50,254,0,0,132,194,162,156,0,255, 		//funkciq 2

	255,0,24,36,254,36,24,0,78,144,144,126,0,254,16,16,254,0,254,24,20,226,0,
	126,64,126,192,0,254,96,24,254,0,140,82,50,254,0,0,68,130,146,108,0,255,			//funckiq 3
	
	
	
	0,255,231,219,1,219,231,255,177,111,111,129,255,1,239,239,1,255,1,231,235,29,255,
	129,191,129,63,255,1,159,231,1,255,115,173,205,1,255,255,251,1,255,255,255,0,  //funkciq 1 inv 

	0,255,231,219,1,219,231,255,177,111,111,129,255,1,239,239,1,255,1,231,235,29,255,
	129,191,129,63,255,1,159,231,1,255,115,173,205,1,255,255,123,61,93,99,255,0,  //funkciq 2 inv 

	0,255,231,219,1,219,231,255,177,111,111,129,255,1,239,239,1,255,1,231,235,29,255,
	129,191,129,63,255,1,159,231,1,255,115,173,205,1,255,255,187,125,109,147,255,0   //funkciq 3 inv
	};

const short int subMenu1L[2]={3,45};			 
unsigned char *pSubMenu1[6];

//
// SubMenu2
//

unsigned char subMenu2[8][45] = {
	255,0,24,36,254,36,24,0,78,144,144,126,0,254,16,16,254,0,254,24,20,226,0,
	126,64,126,192,0,254,96,24,254,0,140,82,50,254,0,0,32,40,254,32,0,255, 			//funkciq 4

	255,0,24,36,254,36,24,0,78,144,144,126,0,254,16,16,254,0,254,24,20,226,0,
	126,64,126,192,0,254,96,24,254,0,140,82,50,254,0,0,78,138,138,114,0,255, 		//funkciq 5

	255,0,24,36,254,36,24,0,78,144,144,126,0,254,16,16,254,0,254,24,20,226,0,
	126,64,126,192,0,254,96,24,254,0,140,82,50,254,0,0,124,146,146,96,0,255,			//funckiq 6
	
	255,0,24,36,254,36,24,0,78,144,144,126,0,254,16,16,254,0,254,24,20,226,0,
	126,64,126,192,0,254,96,24,254,0,140,82,50,254,0,0,2,194,50,14,0,255,			//funckiq 7
	
	
	0,255,231,219,1,219,231,255,177,111,111,129,255,1,239,239,1,255,1,231,235,29,255,
	129,191,129,63,255,1,159,231,1,255,115,173,205,1,255,255,223,215,1,223,255,0,  //funkciq 4 inv 

	0,255,231,219,1,219,231,255,177,111,111,129,255,1,239,239,1,255,1,231,235,29,255,
	129,191,129,63,255,1,159,231,1,255,115,173,205,1,255,255,177,117,117,141,255,0,  //funkciq 5 inv 
																						 
	0,255,231,219,1,219,231,255,177,111,111,129,255,1,239,239,1,255,1,231,235,29,255,
	129,191,129,63,255,1,159,231,1,255,115,173,205,1,255,255,131,109,109,159,255,0,   //funkciq 6 inv

	0,255,231,219,1,219,231,255,177,111,111,129,255,1,239,239,1,255,1,231,235,29,255,
	129,191,129,63,255,1,159,231,1,255,115,173,205,1,255,255,253,61,205,241,255,0   //funkciq 7 inv
	};

const short int subMenu2L[2]={4,45};			 
unsigned char *pSubMenu2[8];


//
// Sub Menu 3
//
unsigned char subMenu3[4][45] = {
	255,0,24,36,254,36,24,0,78,144,144,126,0,254,16,16,254,0,254,24,20,226,0,
	126,64,126,192,0,254,96,24,254,0,140,82,50,254,0,0,108,146,146,108,0,255, 				//funkciq 8

	255,0,24,36,254,36,24,0,78,144,144,126,0,254,16,16,254,0,254,24,20,226,0,
	126,64,126,192,0,254,96,24,254,0,140,82,50,254,0,0,76,146,146,124,0,255, 		//funkciq 9
	
	
	0,255,231,219,1,219,231,255,177,111,111,129,255,1,239,239,1,255,1,231,235,29,255,
	129,191,129,63,255,1,159,231,1,255,115,173,205,1,255,255,147,109,109,147,255,0,  //funkciq 8 inv 

	0,255,231,219,1,219,231,255,177,111,111,129,255,1,239,239,1,255,1,231,235,29,255,
	129,191,129,63,255,1,159,231,1,255,115,173,205,1,255,255,179,109,109,131,255,0,  //funkciq 9 inv 
	};

const short int subMenu3L[2]={2,45};			 
unsigned char *pSubMenu3[4];

//
// Sub Menu 4
//
 unsigned char subMenu4[6][47] = {
	255,0,24,36,254,36,24,0,78,144,144,126,0,254,16,16,254,0,254,24,20,226,0,
	126,64,126,192,0,254,96,24,254,0,140,82,50,254,0,4,254,0,124,130,130,124,0,255, 		//funkciq 10

	255,0,24,36,254,36,24,0,78,144,144,126,0,254,16,16,254,0,254,24,20,226,0,
	126,64,126,192,0,254,96,24,254,0,140,82,50,254,0,4,254,0,0,4,254,0,0,255, 			//funkciq 11

	255,0,24,36,254,36,24,0,78,144,144,126,0,254,16,16,254,0,254,24,20,226,0,
	126,64,126,192,0,254,96,24,254,0,140,82,50,254,0,4,254,0,132,194,162,156,0,255,		//funkciq 12
	
	
	
	0,255,231,219,1,219,231,255,177,111,111,129,255,1,239,239,1,255,1,231,235,29,255,
	129,191,129,63,255,1,159,231,1,255,115,173,205,1,255,251,1,255,131,125,125,131,255,0,  //funkciq 10 inv 

	0,255,231,219,1,219,231,255,177,111,111,129,255,1,239,239,1,255,1,231,235,29,255,
	129,191,129,63,255,1,159,231,1,255,115,173,205,1,255,251,1,255,255,251,1,255,255,0,  //funkciq 11 inv 

	0,255,231,219,1,219,231,255,177,111,111,129,255,1,239,239,1,255,1,231,235,29,255,
	129,191,129,63,255,1,159,231,1,255,115,173,205,1,255,251,1,255,123,61,93,99,255,0   //funkciq 12 inv
	};

const short int subMenu4L[2]={3,47};			 
unsigned char *pSubMenu4[6];

//
//	Function for clearing the second row(submenu) after double click
//
void clear_subMenu(void)
{
	unsigned char clear[96]={0};		  // 96 zeroes
	IntMasterDisable();
	OSRAM96x16x1ImageDraw(clear,0,1,96,1);
	IntMasterEnable();
}
	
//
//	Function for animating the menu when moving to the left
//
void animate_left(unsigned char **menu, const short int len[2], short int pos, short int dept)
{
	 int i;
	 for (i=0; i<len[1]; ++i)
	 {
		OSRAM96x16x1ImageDraw(menu[len[0]+pos],i,dept,len[1],1);     //selected
		OSRAM96x16x1ImageDraw(menu[pos+1],len[1]-1+i,dept,len[1],1);
		OSRAM96x16x1ImageDraw(menu[pos+2],2*(len[1]-1)+i,dept,len[1],1);
		if (pos > 0)
			OSRAM96x16x1ImageDraw(menu[pos-1],-len[1]-1+i,dept,len[1],1);

	 }
}   

//
//	Function for animating the menu when moving to the right
//
void animate_right(unsigned char **menu, const short int len[2], short int pos, short int dept)
{
 int i;
 unsigned char clear[1]={0};
 for (i=0; i<len[1]; ++i)
 {
 	if (pos>1)
	{
		OSRAM96x16x1ImageDraw(menu[pos-2],-i,dept,len[1],1);  
		OSRAM96x16x1ImageDraw(menu[pos-1],len[1]-1-i,dept,len[1],1);
		OSRAM96x16x1ImageDraw(menu[len[0]+pos],2*(len[1]-1)-i,dept,len[1],1);     //selected
		if (pos < len[0]-1)
		{
		OSRAM96x16x1ImageDraw(menu[pos+1],3*(len[1]-1)-i,dept,len[1],1);
		OSRAM96x16x1ImageDraw(clear,4*(len[1]-1)-i+1,dept,2,1);	
		}
		else
		{
			OSRAM96x16x1ImageDraw(clear,3*(len[1]-1)-i+1,dept,2,1);
			 //clear after the last button
		}		  			
	}
	else
	{
		OSRAM96x16x1ImageDraw(menu[pos-1],-i,dept,len[1],1);  
		OSRAM96x16x1ImageDraw(menu[len[0]+pos],len[1]-1-i,dept,len[1],1);     //selected				   	
		if (pos < len[0])
		{
			OSRAM96x16x1ImageDraw(menu[pos+1],2*(len[1]-1)-i,dept,len[1],1);
		}
	}
 }
}

//
//	Redrawing the menu when the position is changed
//
void redrawMenu(unsigned char **menu, const short int len[2], short int pos, short int oldPos, short int dept)
{
	
	IntMasterDisable();
	if (oldPos == 0)		      // on initialisation of menu
	{
		if (pos*(len[1]-1) > 90)
		{
			if (pos < len[0]-1)
			{
				animate_right(menu, len, pos, dept);
				++tempPos;
			}
			else
			{
				animate_right(menu, len, pos-1, dept);
				++tempPos;
				OSRAM96x16x1ImageDraw(menu[pos-1],(pos-tempPos-1)*(len[1]-1),dept,len[1],1);
				OSRAM96x16x1ImageDraw(menu[pos+len[0]],(pos-tempPos)*(len[1]-1),dept,len[1],1);
			}
		}
		else   
		OSRAM96x16x1ImageDraw(menu[pos+len[0]],(pos)*(len[1]-1),dept,len[1],1);
	}

	else if (pos > oldPos-1)
	{
		if (((pos -tempPos+1 )*(len[1]-1) > 90)&&(pos < len[0]-1) )
		{ 
			animate_right(menu,len,pos,dept);
			++tempPos;
		}
		else if (((pos-tempPos+1)*(len[1]-1) > 95) && (pos == len[0]-1))
		{
			animate_right(menu,len,pos,dept);
			++tempPos;
		}
					   
		else 
		{
			
			OSRAM96x16x1ImageDraw(menu[pos-1],(pos-tempPos-1)*(len[1]-1),dept,len[1],1);
			OSRAM96x16x1ImageDraw(menu[len[0]+pos],(pos-tempPos)*(len[1]-1),dept,len[1],1);
		}
	}
	else if (pos < oldPos-1)
	{
		if (((pos-tempPos-1)*(len[1]-1) < 0)&& (pos > 0))
		{
			animate_left(menu, len, pos, dept);	
			--tempPos;
		}
		else 
		{
			OSRAM96x16x1ImageDraw(menu[pos+len[0]],(pos-tempPos)*(len[1]-1),dept,len[1],1);
			OSRAM96x16x1ImageDraw(menu[pos+1],(pos-tempPos+1)*(len[1]-1),dept,len[1],1);	
		}
	}
  

	IntMasterEnable();
}
